﻿using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using Newtonsoft.Json.Linq;

namespace DeskRedis.Util
{
    public static class RedisValueUtil
    {
        /// <summary>
        /// 创建文本框。
        /// </summary>
        /// <returns></returns>
        public static TextBox CreateTextBox()
        {
            TextBox tb = new TextBox();
            tb.TextWrapping = TextWrapping.Wrap;
            tb.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
            return tb;
        }

        /// <summary>
        /// 创建List类型的UCRedisValue。
        /// </summary>
        /// <returns></returns>
        public static DataGrid CreateDataGrid4List()
        {
            DataGrid grid = CreateDataGrid();
            DataGridTextColumn columnValue = new DataGridTextColumn()
            {
                Header = "值",
                Binding = new Binding("."),
                Width = new DataGridLength(1, DataGridLengthUnitType.Star)
            };
            grid.Columns.Add(columnValue);
            return grid;
        }

        /// <summary>
        /// 创建Set类型的UCRedisValue。
        /// </summary>
        /// <returns></returns>
        public static DataGrid CreateDataGrid4Set()
        {
            DataGrid grid = CreateDataGrid();
            DataGridTextColumn columnValue = new DataGridTextColumn()
            {
                Header = "值",
                Binding = new Binding("."),
                Width = new DataGridLength(1, DataGridLengthUnitType.Star)
            };
            grid.Columns.Add(columnValue);
            return grid;
        }

        /// <summary>
        /// 创建Zset类型的UCRedisValue。
        /// </summary>
        /// <returns></returns>
        public static DataGrid CreateDataGrid4Zset()
        {
            DataGrid grid = CreateDataGrid();
            DataGridTextColumn columnValue = new DataGridTextColumn()
            {
                Header = "值",
                Binding = new Binding("Key"),
                Width = new DataGridLength(1, DataGridLengthUnitType.Star)
            };
            DataGridTextColumn columnScore = new DataGridTextColumn()
            {
                Header = "排序号",
                Binding = new Binding("Value"),
                Width = new DataGridLength(100)
            };
            grid.Columns.Add(columnValue);
            grid.Columns.Add(columnScore);

            return grid;
        }

        /// <summary>
        /// 创建hash类型的UCRedisValue。
        /// </summary>
        /// <returns></returns>
        public static DataGrid CreateDataGrid4Hash()
        {
            DataGrid grid = CreateDataGrid();
            DataGridTextColumn columnKey = new DataGridTextColumn()
            {
                Header = "键",
                Binding = new Binding("Key"),
                MinWidth = 170,
                Width = 170
            };
            DataGridTextColumn columnValue = new DataGridTextColumn()
            {
                Header = "值",
                Binding = new Binding("Value"),
                Width = new DataGridLength(1, DataGridLengthUnitType.Star)
            };
            grid.Columns.Add(columnKey);
            grid.Columns.Add(columnValue);

            return grid;
        }

        /// <summary>
        /// 格式化redis的值的文本。
        /// </summary>
        /// <param name="originalValue">原始值。</param>
        /// <param name="viewType">视图类型。TEXT|JSON|HEX</param>
        /// <returns></returns>
        public static string FormatRedisValue(this string originalValue, string viewType)
        {
            if (originalValue == null)
            {
                return null;
            }

            if (string.Equals("TEXT", viewType))
            {
                return originalValue;
            }

            if (string.Equals("JSON", viewType))
            {
                try
                {
                    JToken token = JToken.Parse(originalValue);
                    return token.ToString();
                }
                catch //如果不是json格式的字符串，就会报错，直接返回原文本
                {
                    return originalValue;
                }
            }

            if (string.Equals("HEX", viewType))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(originalValue);
                StringBuilder sb = new StringBuilder();
                foreach (byte item in bytes)
                {
                    sb.Append(item.ToString("X2")).Append(" ");
                }

                return sb.ToString();
            }

            return originalValue;
        }


        #region 私有方法

        /// <summary>
        /// 创建数据表格。
        /// </summary>
        /// <returns></returns>
        private static DataGrid CreateDataGrid()
        {
            DataGrid grid = new DataGrid();
            grid.CanUserAddRows = false;
            grid.CanUserDeleteRows = false;
            grid.CanUserReorderColumns = false;
            grid.CanUserResizeRows = false;
            grid.AlternationCount = 3;
            grid.AutoGenerateColumns = false;
            grid.IsReadOnly = true;

            return grid;
        }

        #endregion
    }
}